﻿--[[
   @brief    login qrcode switch config
   @author   harleyxwang
   @version  1.0
   @date     2016-04-30
   @features
        - load qrcode switch cfg from cgi
--]]

login_qrcode_config = 
{
    -- config url
    cfg_url = LoadStr("GENERAL_CFG_QUERY_URL");
    cancel_update_qrcode = false;
    -- cfg name
    cfg_pkg_name = "/tgp/platform/login/qrcode_config";
    cfg_call_back = "login_qrcode_cfg_call_back";
    cfg_name = "qrcode_switch_state";
    cfg_version = 1;
    is_request_send_succ = false;
    -- local mac address
    local_mac_address = nil;
    -- net bar mode
    netbar_mode = nil;
    -- save cgi result
    qrcode_config_table = {
        regular_channel_id = 0,
        netbar_channel_id = 0,
        regular_show_animation = 0,
        netbar_show_animation = 0,
        regular_force_show = 0,
        netbar_force_show = 0,
        mac_filter_status = 0, -- 3种状态：0/1/2 分别对应关闭、灰度、全量
        mac_filter_list = {},
        new_qrcode_switch = 0,
        qrcode_icon = "",
        qrcode_tips = "",
        qrcode_link_tips = "",
        qrcode_link = "", -- qrcode tips链接跳转地址
        regular_show_prior = 0,
        netbar_show_prior = 0,
        genqr_source_host = "", -- 生成二维码要替换的原始字符串, 当该字段没有配置或者是空的时候则走原来的逻辑
        genqr_replace_txt = "", -- 生成二维码替换字符串
    };

    init = function()
        -- query the cfg
        local post_data = {};
        post_data["version"] = 1;
        post_data["pkg_name"] = login_qrcode_config.cfg_pkg_name;
        post_data["cfg_list"] = login_qrcode_config.cfg_name;
        post_data["callback"] = login_qrcode_config.cfg_call_back;
        login_qrcode_config.is_request_send_succ = base_utility.curl_request_async(
            login_qrcode_config.cfg_url, "login_qrcode_config.on_get_cfg_result",
            false, post_data, true);
        base_utility.log("[login_qrcode_config] send qrcode request result: " .. 
            tostring(login_qrcode_config.is_request_send_succ));
        comm_center.reg_service("Svr_GetNewQRCodeSwitch",
            login_qrcode_config.handle_get_new_qrcode_swtich);
    end;

    -- parse the cfg result
    on_get_cfg_result = function(result)
        base_utility.log("[login_qrcode_config].on_get_cfg_result called: ".. result);
        local success, rsp_table, crc = base_utility.cfg_string_to_table(result, login_qrcode_config.cfg_call_back);
        if not success then
            base_utility.log("[login_qrcode_config] on_get_cfg_result failer.");
            return;
        end
        -- 配置 config_table = {cfg_name:qrcode_switch_state , 
        -- cfg_value:"{"regular_channel_id":1,"netbar_channel_id":1}"}
        local config_table = rsp_table[1];
        local cfg_value = nil;
        if config_table ~= nil and type(config_table) == 'table' and
            config_table["cfg_name"] == login_qrcode_config.cfg_name then
            cfg_value = config_table["cfg_value"];
            base_utility.log("[login_qrcode_config] on_get_cfg_result : " .. cfg_value);
        end
        if cfg_value ~= nil and type(cfg_value) == 'string' then
            local json_table = base_utility.json_to_table(cfg_value);
            if json_table ~= nil then
                if json_table["regular_channel_id"] ~= nil then
                    login_qrcode_config.qrcode_config_table["regular_channel_id"] = 
                        tonumber(json_table["regular_channel_id"]);
                end
                if json_table["netbar_channel_id"] ~= nil then
                    login_qrcode_config.qrcode_config_table["netbar_channel_id"] = 
                        tonumber(json_table["netbar_channel_id"]);
                end
                if json_table["regular_show_animation"] ~= nil then
                    login_qrcode_config.qrcode_config_table["regular_show_animation"] =
                        tonumber(json_table["regular_show_animation"]);
                end
                if json_table["netbar_show_animation"] ~= nil then
                    login_qrcode_config.qrcode_config_table["netbar_show_animation"] = 
                        tonumber(json_table["netbar_show_animation"]);
                end
                if json_table["regular_force_show"] ~= nil then
                    login_qrcode_config.qrcode_config_table["regular_force_show"] = 
                        tonumber(json_table["regular_force_show"]);
                end
                if json_table["netbar_force_show"] ~= nil then
                    login_qrcode_config.qrcode_config_table["netbar_force_show"] = 
                        tonumber(json_table["netbar_force_show"]);
                end
                if json_table["mac_filter_status"] ~= nil then
                    login_qrcode_config.qrcode_config_table["mac_filter_status"] = 
                        tonumber(json_table["mac_filter_status"]);
                end
                if json_table["mac_filter_list"] ~= nil then
                    login_qrcode_config.qrcode_config_table["mac_filter_list"] = 
                        json_table["mac_filter_list"];
                end
                if nil ~= json_table["new_qrcode_switch"] then
                    login_qrcode_config.qrcode_config_table["new_qrcode_switch"] = 
                        json_table["new_qrcode_switch"];
                    base_utility.log(string.format(
                        "[login_qrcode_config][on_get_cfg_result]new_qrcode_switch:%d",
                        json_table["new_qrcode_switch"]));
                end;
                if nil ~= json_table["qrcode_icon"] then
                    login_qrcode_config.qrcode_config_table["qrcode_icon"] = 
                        json_table["qrcode_icon"];
                    base_utility.log(string.format(
                        "[login_qrcode_config][on_get_cfg_result]qrcode_icon:%s",
                        json_table["qrcode_icon"]));
                end;
                if nil ~= json_table["qrcode_tips"] then
                    login_qrcode_config.qrcode_config_table["qrcode_tips"] = 
                        json_table["qrcode_tips"];
                    base_utility.log(string.format(
                        "[login_qrcode_config][on_get_cfg_result]qrcode_tips:%s",
                        json_table["qrcode_tips"]));
                end
                if nil ~= json_table["qrcode_link_tips"] then
                    login_qrcode_config.qrcode_config_table["qrcode_link_tips"] = 
                        json_table["qrcode_link_tips"];
                    base_utility.log(string.format(
                        "[login_qrcode_config][on_get_cfg_result]qrcode_link_tips:%s",
                        json_table["qrcode_link_tips"]));
                end
                if nil ~= json_table["qrcode_link"] then
                    login_qrcode_config.qrcode_config_table["qrcode_link"] = 
                        json_table["qrcode_link"];
                    base_utility.log(string.format(
                        "[login_qrcode_config][on_get_cfg_result]qrcode_link:%s",
                        json_table["qrcode_link"]));
                end
                if json_table["regular_show_prior"] ~= nil then
                    login_qrcode_config.qrcode_config_table["regular_show_prior"] = 
                        tonumber(json_table["regular_show_prior"]);
                end
                if json_table["netbar_show_prior"] ~= nil then
                    login_qrcode_config.qrcode_config_table["netbar_show_prior"] = 
                        tonumber(json_table["netbar_show_prior"]);
                end
                if nil ~= json_table["genqr_source_host"] then
                    login_qrcode_config.qrcode_config_table["genqr_source_host"] = 
                        json_table["genqr_source_host"];
                end
                if nil ~= json_table["genqr_replace_txt"] then
                    login_qrcode_config.qrcode_config_table["genqr_replace_txt"] =
                        json_table["genqr_replace_txt"];
                end
            end
        end

        -- 抛出事件通知
        event_center.send_event(login_event.LOGIN_QRCODE_CFG_SWITCH, 
            base_utility.table_to_json(login_qrcode_config.qrcode_config_table));
    end;

    is_netbar = function(args)
        if login_qrcode_config.netbar_mode == nil then
            local gl = base_utility.get_component_adapter("IGame_library",
                        "ierd_tgp.game_library.Get_game_library_adapter();");
            local cid = gl:get_bind_channel_id();
            login_qrcode_config.netbar_mode = false;
            if 1 == cid then
                login_qrcode_config.netbar_mode = true;
            end
        end
        return login_qrcode_config.netbar_mode or false;
    end;

    show_qrcode_switch = function()
        local qrcode_switch = 0;
        if login_qrcode_config.is_netbar() then
            qrcode_switch = login_qrcode_config.qrcode_config_table["netbar_channel_id"];
        else
            qrcode_switch = login_qrcode_config.qrcode_config_table["regular_channel_id"];
        end
        base_utility.log("[login_qrcode_config], show_qrcode_switch: " .. tostring(qrcode_switch));
        return qrcode_switch;
    end;

    show_qrcode_animation = function()
        local animation_switch = 0;
        if login_qrcode_config.is_netbar() then
            animation_switch = login_qrcode_config.qrcode_config_table["netbar_show_animation"];
        else
            animation_switch = login_qrcode_config.qrcode_config_table["regular_show_animation"];
        end
        return animation_switch;
    end;

    force_show_guide_switch = function()
        local guide_switch = 0;
        local netbar_force_show = login_qrcode_config.qrcode_config_table["netbar_force_show"];
        local regular_force_show = login_qrcode_config.qrcode_config_table["regular_force_show"];
        local mac_filter_switch = login_qrcode_config.mac_filter_switch();
        if login_qrcode_config.is_netbar() then
            if netbar_force_show == 1 and mac_filter_switch == 1 then
                guide_switch = 1;
            else
                guide_switch = 0;
            end
        else
            if regular_force_show == 1 and mac_filter_switch == 1 then
                guide_switch = 1;
            else
                guide_switch = 0;
            end 
        end
        return guide_switch;
    end;

    get_local_mac_address = function()
        if login_qrcode_config.local_mac_address ~= nil then
            return login_qrcode_config.local_mac_address;
        end
        local local_mac_address = nil;
        local login_mgr =
            base_utility.get_component_adapter("ILogin_mgr",
                "ierd_tgp.login_mgr.GetLoginMgr();");
        if login_mgr ~= nil then
            local_mac_address = NewStr(16);
            login_mgr:GetMacAddress(local_mac_address, 16);
            base_utility.log("[login_qrcode_config] local_mac_address is -> " .. local_mac_address);
        end
        login_qrcode_config.local_mac_address = local_mac_address;
        return login_qrcode_config.local_mac_address;
    end;

    check_mac_address = function()
        local is_mac_number_match = false;
        local local_mac_address = login_qrcode_config.get_local_mac_address();
        if local_mac_address == nil then
            base_utility.log("[login_qrcode_config] get loacl mac address failed" );
            return is_mac_number_match;
        end
        local local_mac_address_last_number = string.lower(string.sub(local_mac_address,12,12));
        base_utility.log("[login_qrcode_config] local_mac_address_last_number is " .. 
            local_mac_address_last_number);
        local mac_filter_list = login_qrcode_config.qrcode_config_table["mac_filter_list"];
        if mac_filter_list == nil or local_mac_address_last_number == nil then
            return is_mac_number_match;
        end
        -- 匹配本地mac地址最后一个字符
        for key,value in ipairs(mac_filter_list) do
            if nil ~= value then
                local str_value = string.lower(tostring(value));
                if str_value == local_mac_address_last_number then
                    is_mac_number_match = true;
                    break;
                end
            end
        end
        base_utility.log("[login_qrcode_config] match the last mac number ->" .. 
            tostring(is_mac_number_match));
        return is_mac_number_match;
    end;

    mac_filter_switch = function()
        local mac_filter_status = login_qrcode_config.qrcode_config_table["mac_filter_status"];
        -- mac_filter_status 状态设置：0/1/2  分别对应：关闭、灰度、全量打开
        base_utility.log("[login_qrcode_config] mac_filter status is " .. mac_filter_status);
        local mac_filter_switch = 0;
        if mac_filter_status == 1 then
            if login_qrcode_config.check_mac_address() == true then
                mac_filter_switch = 1;
            else
                mac_filter_switch = 0;
            end 
        elseif mac_filter_status == 2 then
            mac_filter_switch = 1;
        else
            mac_filter_switch = 0;
        end
        base_utility.log("[login_qrcode_config] mac_filter_switch is " .. mac_filter_switch);
        return mac_filter_switch;
    end;

    handle_get_new_qrcode_swtich = function (data)
        local out = {};
        out.is_open = false;
        if 1 == login_qrcode_config.qrcode_config_table["new_qrcode_switch"] then
            out.is_open = true;
        end
        out.source_host = login_qrcode_config.qrcode_config_table["genqr_source_host"];
        out.replace_txt = login_qrcode_config.qrcode_config_table["genqr_replace_txt"];
        comm_center.call_service("Svr_ShowLoginNavTab", { show = false });
        return out;
    end;

    qrcode_prior_show = function(args)

        if 1 == login_qrcode_config.qrcode_config_table.netbar_show_prior and
               login_qrcode_config.is_netbar() then
               return true;
        end

        if 1 == login_qrcode_config.qrcode_config_table.regular_show_prior and
            not login_qrcode_config.is_netbar() then
                return true;
        end

        return false;
    end;

};
login_qrcode_config.init();